package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.File;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

/**
 * @author zhoudan
 * @date 2016年1月13日
 */
public class MyAddFormActivity extends MyDemandActivity {

    private LinearLayout carrier_layout;

    private BasicProperties bp;
    private File file;
    private String fkey;
    private int fvalue;
    private String rowid;
    private String DKID;

    private String title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_pager_carrier_main);
    }

    @Override
    public void initView() {
        carrier_layout = (LinearLayout) findViewById(R.id.carrier_layout);
        fkey = "rowid";
        file = (File) getIntent().getSerializableExtra("file");
        bp = (BasicProperties) getIntent().getSerializableExtra("bp");
        rowid = getIntent().getStringExtra("rowid");
        DKID = getIntent().getStringExtra("DKID");
        f3 = getIntent().getBooleanExtra("f3", true);
        pkgPath = getIntent().getStringExtra("pkgPath");
        transPath = pkgPath + "/TASK/TRANSPORT/";
        xmlPath = pkgPath + "/DEMAND/";
        dbPath = transPath + "DATA.db";
        dBManagerUtil = DBManagerUtil.getInstance(transPath, this);
        if (bp != null) {
            title = bp.getTitle();
        } else {
            title = file.getName();
        }

        if (StringUtil.isNotEmpty(rowid)) {
            title = title.replace("新增", "") + "详情";
            fvalue = Integer.parseInt(rowid);
        } else if (!title.contains("增")) {
            title = "新增" + title;
        }

        setTitle(title);
        addMap.clear();
        //显示保存按钮
        LinearLayout save_layout = (LinearLayout) findViewById(R.id.save_layout);
        save_layout.setVisibility(View.VISIBLE);
        save_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String table = CommenUtil.getTableName(file.getTable(), Constants.currentCycle.getLabel());
                int i = saveData(file, table, DKID);
                switch (i) {
                    case 0:
                        return;
                    case 1:
                        CommenUtil.toast("保存成功！", MyAddFormActivity.this);
                        Intent intent = new Intent(MyAddFormActivity.this, MyDemandActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("f3", false);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
                        break;
                    case 2:
                        CommenUtil.toast("保存失败！", MyAddFormActivity.this);
                        break;

                    default:
                        break;
                }
            }
        });

        initFormView(file, bp, findViewById(R.id.contentArea), carrier_layout, fkey, fvalue);
        setmOnBackClickListener(new onBackClickListener() {
            @Override
            public void onBackClick() {
                if (!isClickSave && !addMap.isEmpty() && !formDataMap.equals(addMap)) {
                    isAlertDown(MyAddFormActivity.this, "是否撤销此次编辑？", "确定", "取消", isCancle,
                            null, null, null, -1, null);
                } else {
                    finish();
                }
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isClickSave && !addMap.isEmpty() && !formDataMap.equals(addMap)) {
                isAlertDown(MyAddFormActivity.this, "是否撤销此次编辑？", "确定", "取消", isCancle,
                        null, null, null, -1, null);
            } else {
                finish();
            }
        }
        return true;
    }

}
